﻿#region Using

using System.Dynamic;

using Common.Base;
using Common.Enums;

using GameCommon.Base;
using GameCommon.ComponentInterfaces;
using GameCommon.Components;
using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using Menu.Common;

using Xna = Microsoft.Xna.Framework;

using GlobalServices = GameCommon.Services.Service;

#endregion

namespace Menu.Components
{
    internal class Text : MenuComponentBase, ITextHitBox, IStackable
    {
        public Text(string id)
            : base(id)
        {
            this.Stackable = true;
        }

        public Text(string id, MenuComponentBase objectToCopy, FontEnum font, FontSizeEnum size)
            : base(id, objectToCopy)
        {
            this.Stackable = true;
            this.Copy(objectToCopy);
            this.Font = Managers.FontManager.GetFont(font, size);
        }

        protected Text(string id, Text objectToCopy, FontEnum font, FontSizeEnum size)
            : base(id, objectToCopy)
        {
            this.Stackable = true;
            this.Copy(objectToCopy);
            this.Font = Managers.FontManager.GetFont(font, size);
        }

        public int? LineHeight { get; set; }
        
        public HorizontalAlignment HorizontalAlignment { get; set; }

        public VerticalAlignment VerticalAlignment { get; set; }

        public Font Font { get; private set; }

        public IHitBox HitBox { get; set; }

        public bool Selected { get; set; }

        public bool Stackable { get; set; }

        public ToolTip ToolTip { get; set; }

        public string Value { get; set; }

        public override void Settle()
        {
            base.Settle();
            this.Dimension = XmlHelper.GetDimension(this, this.Parent);

            GlobalServices.ComponentModifier.ApplyAlignments(this.Dimension, this.Parent.Dimension, this.Offset, this.HorizontalAlignment, this.VerticalAlignment);

            if (!string.IsNullOrEmpty(this.Command))
            {
                this.HitBox = GlobalServices.ComponentModifier.GetTextHitBox(this, this.Command);
            }
        }

        public override void Draw(IDrawingManager manager, Xna.GameTime gameTime)
        {
            if (this.ToolTip != null && this.HitBox.IsMouseOver)
            {
                this.ToolTip.Draw(manager, gameTime);
            }

            this.DrawingService.DrawFont(manager, this.Font, this.Dimension, this.Value, this.Opacity, this.Layer);
        }

        public override string ToString()
        {
            return string.Format("Text, Id: {0}", this.Id);
        }

        private void Copy(Text objectToCopy)
        {
            this.Value = objectToCopy.Value;
            this.LineHeight = objectToCopy.LineHeight;
            this.ToolTip = objectToCopy.ToolTip;
        }
    }
}